package com.lin.controller;

import com.lin.VO.ResultVO;
import com.lin.dataobject.TSRegion;
import com.lin.enums.ResultEnum;
import com.lin.exception.SellException;
import com.lin.form.RegionForm;
import com.lin.server.RegionService;
import com.lin.utils.KeyUtils;
import com.lin.utils.ResultVOUtils;
import com.lin.utils.StringUtils;
import com.lin.utils.treeuntil.RegionTreeUntil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/9/21.
 */
@Controller
@RequestMapping("/region")
public class SellerRegionController {
    @Autowired
    private RegionService regionService;

    @RequestMapping("treeView")
    public ModelAndView treeView(){
        return new ModelAndView("region/index1");
    }

//    @RequestMapping("getTreeData")
//    @ResponseBody
//    public List getTreeData(){
//        List list=null;
//        try{
//            List<TSRegion> institutions = regionService.findAll();
//            RegionTreeUntil regionTreeUntil =new RegionTreeUntil();
//            list= regionTreeUntil.fillTree(institutions);
//
//        }catch(Exception e) {
//            e.printStackTrace();
//        }
//        return list;
//    }

    @RequestMapping("/view")
    public ModelAndView addView(@RequestParam(value = "id",required = false) String id,
                                Map<String,Object> map){
        if (StringUtils.isNotEmpty(id)){
            TSRegion tsRegion= regionService.findById(id);
            map.put("region",tsRegion);
        }
        return new ModelAndView("region/add_view");
    }

    @ResponseBody
    @PostMapping("/save")
    public ResultVO save(@Valid RegionForm form, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            return ResultVOUtils.error(ResultEnum.PARAM_ERROR.getCode(),
                    bindingResult.getFieldError().getDefaultMessage());
        }
        TSRegion tsRegion=new TSRegion();
        try {
            if (StringUtils.isNotEmpty(form.getId())){
                tsRegion= regionService.findById(form.getId());
            }
            else{
                form.setId(KeyUtils.getUniqueKey());
            }
            if (!StringUtils.isNotEmpty(form.getPid())){
                form.setPid("0");
            }
            BeanUtils.copyProperties(form,tsRegion);
            regionService.save(tsRegion);
        }catch (SellException e){
            return ResultVOUtils.error(ResultEnum.SAVE_ERROR.getCode(),
                    ResultEnum.SAVE_ERROR.getMessage());
        }
        return ResultVOUtils.success("保存成功");
    }

    @ResponseBody
    @PostMapping("del")
    public ResultVO del(@RequestParam(value = "id",required = false) String id){
        TSRegion region= regionService.findById(id);
        if (region==null){
            return ResultVOUtils.error(ResultEnum.NO_FOUND.getCode(),
                    ResultEnum.NO_FOUND.getMessage());
        }
        Long childrenCount= regionService.getChildrenCount(region.getId());
        if (childrenCount>0){
            return ResultVOUtils.error(ResultEnum.HAVE_CHILDREN.getCode(),
                    ResultEnum.HAVE_CHILDREN.getMessage());
        }
        regionService.del(region);
        return ResultVOUtils.success("删除成功");
    }

    @RequestMapping("/getAllData")
    @ResponseBody
    public List<TSRegion> getAllData(){
        List<TSRegion> list=regionService.findAll();
        return list;
    }
}
